/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare;

import java.text.MessageFormat;

import java.util.ResourceBundle;


/**
 * Builds a log message from the faare_messages.properties file
 *  (located in the Classes directory) and one or several parameters.
 *
 */
public final class MessageUtil
{
    /** Utility class. */
    private MessageUtil()
    {
    }

    /** Message file properties. */
    private static ResourceBundle _myResources = null;

    /**
     * Gets a message with no parameter.
     *
     * @param messageKey message key
     *
     * @return corresponding message or null if not found.
     */
    public static String getMessageString(String messageKey)
    {
        try
        {
            return _myResources.getString(messageKey);
        }
        catch (Exception e)
        {
            return null;
        }
    }

    /**
     * Initialization (called by PropertiesManager).
     *
     * @param myResources Message file properties
     */
    public static void initialize(ResourceBundle myResources)
    {
        if (myResources == null)
        {
            throw new RuntimeException(
                "MessageUtil initialization : ResourceBundle is null");
        }

        _myResources = myResources;
    }

    /**
     * Generates a message with no parameter. Message key must exist.
     *
     * @param messageKey message key
     * @return formatted message
     */
    public static String formatMessage(String messageKey)
    {
        MessageFormat mf = new MessageFormat(getMessageString(messageKey));

        return mf.format(new Object[0]);
    }

    /**
     * Generates a message with one parameter. Message key must exist.
     *
     * @param messageKey message key
     * @param arg0 parameter
     * @return formatted message
     */
    public static String formatMessage(String messageKey, Object arg0)
    {
        MessageFormat mf = new MessageFormat(getMessageString(messageKey));

        Object[] args = new Object[1];
        args[0] = arg0;

        return mf.format(args);
    }

    /**
     * Generates a message with two parameters. Message key must exist.
     *
     * @param messageKey message key
     * @param arg0 first parameter
     * @param arg1 second parameter
     * @return formatted message
     */
    public static String formatMessage(String messageKey, Object arg0,
        Object arg1)
    {
        MessageFormat mf = new MessageFormat(getMessageString(messageKey));
        Object[] args = new Object[2];
        args[0] = arg0;
        args[1] = arg1;

        return mf.format(args);
    }

    /**
     * Generates a message with three parameters. Message key must exist.
     *
     * @param messageKey message key
     * @param arg0 first parameter
     * @param arg1 second parameter
     * @param arg2 third parameter
     * @return formatted message
     */
    public static String formatMessage(String messageKey, Object arg0,
        Object arg1, Object arg2)
    {
        MessageFormat mf = new MessageFormat(getMessageString(messageKey));
        Object[] args = new Object[3];
        args[0] = arg0;
        args[1] = arg1;
        args[2] = arg2;

        return mf.format(args);
    }
}
